package Singleton;

/**
 * @ProjectName: 23种设计模式
 * @Package: Singleton
 * @ClassName: Mgr06
 * @Author: renhan
 * @Description:
 * @Date: 2020/11/10 16:25
 * @Version: 1.0
 *
 * 完美写法
 */
public class Mgr06 {
    private static Mgr06 INSTANCE;

    private Mgr06() {
    }

    public static  Mgr06 getInstance() {
        // 会被抢先
        if (INSTANCE == null) {
            synchronized (Mgr06.class) {
                if (INSTANCE == null) {
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    INSTANCE = new Mgr06();
                }
            }
        }
        return INSTANCE;
    }

    public void m() {
        System.out.println("m");
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                System.out.println(Mgr06.getInstance().hashCode());
            }).start();
        }
    }
}
